import { observable, action, autorun, toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import {WeaForm, WeaTableNew} from 'comsMobx'
import {message, Button,Modal } from "antd";
import * as Apis from '../../apis/infoWord';
import { ListStore } from '../listStore';
import {Text} from '../../components/util/text';
const text = new Text();
const {TableStore} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
export default class infoWordMergeStore extends ListStore {

    @observable responseData = {};
    @observable title = "";
    @observable status = {
        loading: false,
        cbsjVisible: false,//出版时间弹出框是否显示
    }
    @observable loadingSpin = true;     //全局loading
    @observable isReload = 0;           //是否重新生成

    journalid = "";                     //期刊id
    @observable docid;
    @observable docstatus;              //状态
    @observable columnData = [];        //栏目和内容
    @observable column = [];            //栏目信息 
    @observable psList = [];            //批示信息
    @observable periods = "";           //期数
    @observable totalperiods = "";      //总期数
    @observable src = "";
    @observable mTemplate = "";
    @observable mFileType = "";

    @observable cbsj = "";  //出版时间
    @observable cbsjtemp = "";  //出版时间(临时存储)
    
    constructor(props) {
        super(props);
    }

    setState = (params = {}) => {
        Object.keys(params).forEach(o => {
            this.status[o] = params[o];
        })
        return this;
    }

    @action
    getInfoWordMergeDetail = (params = {}) => {
        let newParams = {};
        this.loadingSpin = true;
        if (params.journalid)
        this.journalid = params.journalid;
        newParams = {...params}
        Apis.getInfoWordMergeDetail(newParams).then(data => {
            if (data.api_status == '1') {
                this.loadingSpin = false;
                this.responseData = { rightMenus: data.rightMenus };
                this.columnData = data.columnData;
                this.column = data.column;
                this.psList = data.psList;
                
                this.title = data.mFileName;
                this.mTemplate = data.mTemplate;
                this.mFileType = data.mFileType;
                this.periods = data.periods;
                this.totalperiods = data.totalperiods;
                this.docid = data.kwdocid;


                window.mTemplate = this.mTemplate;
                window.columnData = toJS(this.columnData);
                window.column = toJS(this.column);      //重新生成时用该数据重新生成信息
                window.psList = toJS(this.psList);
                window.periods = this.periods;
                window.totalperiods = this.totalperiods;
                window.docid = this.docid;
                if(this.mFileType && !this.mFileType.startsWith(".") ){
                    this.mFileType = "."+this.mFileType;
                }
                
                this.src = "/infoJournal/infoWordMerge.jsp?journalid="+this.journalid+"&docid="+this.docid+"&mTemplate="+this.mTemplate+"&mFileType="+this.mFileType+"&mEditType=1&mFileName="+this.title;
            } else {
                message.error(data.api_errormsg);
            }

        })
    }
    getRightMenu = () => {
        const { rightMenus } = this.responseData;
        let btnArr = [];
        rightMenus && rightMenus.length > 0 && rightMenus.map(m => {
            if( m.type == 'BTN_SUBMIT'){
                btnArr.push({
                    key: m.type,
                    icon: <i className={m.menuIcon} />,
                    content: m.menuName,
                    disabled :this.docid && parseInt(this.docid)> 0 ? false : true,
                    onClick: key => {
                        this.buttonClick(key);
                    }
                })
            }else{
                btnArr.push({
                    key: m.type,
                    icon: <i className={m.menuIcon} />,
                    content: m.menuName,
                    onClick: key => {
                        this.buttonClick(key);
                    }
                })
            }
            
        });
        return btnArr;
    }

    /**
   * 上方按钮
   */
    getTopButton = () => {
        const { rightMenus } = this.responseData;
        let btnArr = [];
        btnArr.push(
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@z4be8n@BTN_RELOAD`} style={{ marginLeft: "10px" }} type={"primary"}
                onClick={() => {
                    this.buttonClick("BTN_RELOAD");
                }}>
                {"重新生成"}
            </Button>
        );
        btnArr.push(
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@z4be8n@BTN_RELOAD`} style={{ marginLeft: "10px" }} type={"primary"}
                onClick={() => {
                    this.buttonClick("BTN_UPDATE_CBSJ");
                }}>
                {"修改出版时间"}
            </Button>
        );
        rightMenus && rightMenus.length > 0 && rightMenus.map((m,index) => {
            if( m.type == 'BTN_SUBMIT'){
                btnArr.push(
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@bi30i5@${index}`} style={{ marginLeft: "10px" }} type={"primary"} 
                        disabled={this.docid && parseInt(this.docid)> 0 ? false : true}
                        onClick={() => {
                            this.buttonClick(m.type);
                        } }>
                        {"发送"}
                    </Button>
                );
            }else{
                btnArr.push(
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@bi30i5@${index}`} style={{ marginLeft: "10px" }} type={"primary"} 
                        onClick={() => {
                            this.buttonClick(m.type);
                        } }>
                        {m.menuName}
                    </Button>
                );
            }
            
            
        });
        return btnArr;
    }

    buttonClick(key) {
        if (key == "BTN_SAVE") {
            this.saveInfoDoc();
        }else if(key =='BTN_SUBMIT'){
            this.createWorkFlow();
        }else if(key =='BTN_RELOAD'){
            this.reloadDoc();
        }else if(key =='BTN_UPDATE_CBSJ'){
            this.setState({ cbsjVisible: true })
        }
    }
    //修改出版时间
    @action
    changeCbsj = (val) => {
        this.cbsjtemp = val;
    }
    //更新出版时间
    @action
    updateCBSJ = () =>{
        this.cbsj = this.cbsjtemp;
        window.hideIframe.updateCBSJ(this.cbsj);
        this.setState({ cbsjVisible: false });
    }
    /**
     * 成文保存
     */
    @action
    saveInfoDoc = (params = {}) => {
        const docDatas = window.hideIframe.SaveDocument(this.isReload);
        if (docDatas.off_status == 1 && this.journalid) {
            const params = {
                journalid: this.journalid, docid: this.docid || "",actiontype:"saveDoc", docSubject: this.title, docmain: this.title, maindoc: -1,
                seccategory: '', isOffice: 1, operate: "submit", docPublishType: 2, doccontent: "",cbsj:this.cbsj, ...docDatas
            };
            Apis.saveInfoDoc(params).then(data => {
                if (data.api_status == '1') {
                    message.success(text.SAVESUCCESS);
                    if(data.docid ){
                        this.docid = data.docid;
                        parent.window.location.reload();
                    }
                } else {
                    message.error("error");
                }
            })
        } else {
            message.error(text.OPERATEFAIL);
        }
    }
    /**
     * 提交公文流程
     */
    @action
    createWorkFlow = () => {
        const params = {journalid: this.journalid,actiontype:"createWorkFlow"};
        Apis.saveInfoDoc(params).then(data => {
            if (data.api_status == '1') {
                message.success(text.SAVESUCCESS);
                if( data.requestid){
                    parent.window.location.href ="/workflow/request/ViewRequestForwardSPA.jsp?requestid="+data.requestid;
                }
            } else {
                message.error("error");
            }
        })
    
    }
     /**
     * 重新生成reload
     */
    reloadDoc = () => {
        let _this = this;
        Modal.confirm({
            title: text.DOSURE,
            content: text.DOSURE2,
            onOk() {
                _this.src = "/infoJournal/infoWordMerge.jsp?journalid="+_this.journalid+"&docid="+_this.docid+"&mTemplate="+_this.mTemplate+"&mFileType="+_this.mFileType+"&mEditType=1&mFileName="+_this.title+"&reload=1";
                document.getElementById("hideIframe").src = _this.src;
            },
            onCancel() {},
        })
    
    }

}